﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TaDMS.Models;

namespace TaDMS.Controllers
{
    [Authorize(Roles = "Admin, TruongPhong, PhoPhong, Editer")]
    public class ApiDeviceCategoryController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetAll()
        {
            var data = _unitOfWork.DeviceCategory.GetDeviceCategoryTree();

            var result = new
            {
                success = true,
                DeviceCategory1 = data
            };

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetComboData(int start, int limit, string query)
        {
            int? total;
            var result = String.IsNullOrEmpty(query) || query.Trim().ToUpper().Equals("ALL") ?
                _unitOfWork.DeviceCategory.GetData(total: out total, isPaging: true, start: start, limit: limit, orderBy: order => order.OrderBy(d => d.Name)) :
                _unitOfWork.DeviceCategory.GetData(total: out total, isPaging: true, start: start, limit: limit, orderBy: order => order.OrderBy(d => d.Name), filter: d => d.Name.ToUpper().Contains(query.Trim().ToUpper()));

            var deviceCategorys = result as List<DeviceCategory> ?? result.ToList();
            if (deviceCategorys.Any() && String.IsNullOrEmpty(_unitOfWork.DeviceCategory.ErrorMessage))
            {
                if (total > 0)
                {
                    var success = new
                    {
                        total = total,
                        success = true,
                        data = deviceCategorys,
                        message = "success"
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, success);
                }

                var empty = new
                {
                    total = 0,
                    success = true,
                    message = "data is empty"
                };
                return Request.CreateResponse(HttpStatusCode.OK, empty);
            }

            var error = new
            {
                success = false,
                message = "False. " + _unitOfWork.DeviceCategory.ErrorMessage
            };
            return Request.CreateResponse(HttpStatusCode.OK, error);
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Create(DeviceCategory entity)
        {
            var result = _unitOfWork.DeviceCategory.Create(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.DeviceCategory.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Created"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Craete false: " + _unitOfWork.DeviceCategory.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Update(DeviceCategory entity, string fields)
        {
            var result = _unitOfWork.DeviceCategory.Update(entity, fields);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.DeviceCategory.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Updated"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Update false: " + _unitOfWork.DeviceCategory.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Delete(DeviceCategory entity)
        {
            var result = _unitOfWork.DeviceCategory.Delete(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.DeviceCategory.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Deleted"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Delete false: " + _unitOfWork.DeviceCategory.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }
    }
}
